<?php

/**
 *  私人定制的订单下载
 *  date : 2020-08-25
 *  /Applications/XAMPP/xamppfiles/bin/php  /Applications/MAMP/htdocs/rmsdaemon/index.php Home/privateorder/getBjWebOrder
 *  /alidata/server/php/bin/php /home/rmsdaemon/index.php  Home/privateorder/getBjWebOrder
 */

namespace Home\Controller;

use Think\Controller;
use Think\Log;

class PrivateOrderController extends Controller
{
    protected $conn;
    protected $connBj;
    protected $logFile = '';
    protected $domain = '';
    protected $domains = array();
    protected $cityIds = array(); //对应新APP城市id
    protected $orderApi = 'https://private.lihua.com/api/lhorder/lists';
    protected $statusApi ='https://private.lihua.com/api/lhorder/updatestatus';
    protected $orderCollectApi = 'https://private.lihua.com/api/lhorder/getCollectOrder';
    protected $statusCollectApi ='https://private.lihua.com/api/lhorder/updateCollectState';

    public function _initialize()
    {
        $this->conn = C('RMS_CONNECTSTR');
        $this->connBj = C('BJRMS_CONNECTSTR');
        $this->domains = array(
            1 => 'bj.lihuaerp.com', 2 => 'sh.lihuaerp.com', 3 => 'cz.lihuaerp.com', 4 => 'nj.lihuaerp.com',
            5 => 'sz.lihuaerp.com', 6 => 'wx.lihuaerp.com', 7 => 'gz.lihuaerp.com',
        );
        $this->cityIds = array('bj' => 1, 'sh' => 2, 'cz' => 3, 'nj' => 4, 'sz' => 5, 'wx' => 6, 'gz' => 7);
    }





    // 获取订单/Home/LihuaApp/getOrder/city/cz
    public function getOrder()
    {
        $this->logFile = C('LOG_PATH') . 'privateGetOrder-'  . '-' . date('y_m_d') . '.log';
        $res = $this->httpReq($this->orderApi, array('status' => 2, 'exporttype' => 2));
        $res = json_decode($res, true);
        if (json_last_error()) {
            $msg = "订单数据JSON解析失败";
            $this->addLog($msg);
            die($msg);
        }
        if ($res['code'] == 0) {
            $this->addLog($res['msg']);
            die($res['msg']);
        }
        $data = $res['data']; // 订单数据
        //var_dump($data);
        foreach ($data as $value) {
            $conn = $value['orderList']['city_id'] == 1 ? $this->connBj : $this->conn;
            $orderList = isset($value['orderList']) ? $value['orderList'] : null;
            $goodsList = isset($value['goodsList']) ? $value['goodsList'] : null;
            $payList = isset($value['payList']) ? $value['payList'] : null;
            $actList = isset($value['actList']) ? $value['actList'] : null;

            
            // 订单数据写入RMS
            $addRes = $this->add2rms($orderList, $goodsList, $payList, $actList, $conn);

            

            // 更新来源订单状态为已确认
            $res = $this->httpReq(
                $this->statusApi,
                array(
                    'sn' => $orderList['sn'], //订单号
                    'status' => 3, //状态类型
                    'des' => '订单确认', //配送状态描述
                )
            );
            $res = json_decode($res, true);
            if ($res['code'] == 0) {
                $this->addLog("修改订单{$value['sn']}状态为 订单确认 失败：{$res['msg']}");
            }
        }
    }

    // 将订单写入RMS
    protected function add2rms($row, $goodsList, $payList, $actList, $conn)
    {
        $orderModel = M('orderform', 'rms_', $conn); //订单表
        $goodsModel = M('orderproducts', 'rms_', $conn); //订单商品表
        $payModel = M('orderpayment', 'rms_', $conn); //支付表
        $financeModel = M('orderfinance', 'rms_', $conn); //结账单
        $stateModel = M('orderstate', 'rms_', $conn); //状态表
        $actionModel = M('orderaction', 'rms_', $conn); //日志表
        $yingshouModel = M('orderyingshouexchange', 'rms_', $conn); //营收状态表
        $webstatusModel = M('webstatus', 'rms_', $conn); //网站状态表
        $invoiceModel = M('invoice', 'rms_', $conn); //发票表
        $notifyModel = M('notify_client', 'rms_', $conn); //客户消息


        // 写订单表
        $this->domain = $domain = $this->domains[$row['city_id']]; //配送地区标识
        //list($custdate, $custtime) = explode(' ', $row['deliver_time']);

        //春节期间门店调整
        // $value=='金桥' || $value=='张江' || $value=='陆家嘴' || $value=='宝山' || $value=='杨浦'
        //北京地区
        if ($row['store_name'] == '望京') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '东五环') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '农展店') {
            $row['store_name'] = '';
        }
        // if($row['store_name'] == '光彩'){  $row['store_name']  = '';  }
        // if($row['store_name'] == '朝阳门'){  $row['store_name']  = '';  }
        if ($row['store_name'] == '丽华快餐') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '王府井') {
            $row['store_name'] = '';
        }

        //上海地区
        if ($row['store_name'] == '金桥') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '张江') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '陆家嘴') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '宝山') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '杨浦') {
            $row['store_name'] = '';
        }
        //无锡地区
        if ($row['store_name'] == '金泰') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '太湖') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '凤翔') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '新区') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '人民路') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '清扬') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '五爱') {
            $row['store_name'] = '';
        }
        //南京地区
        if ($row['store_name'] == '白下路') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '黄山路') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '徐庄') {
            $row['store_name'] = '';
        }

        //上海地区
        if ($row['store_name'] == '延安') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '张江') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '合川') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '真北') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '虹口') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '陆家嘴') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '外滩') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '宝山') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '国展') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '杨浦') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '徐汇') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '普陀') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '浦东') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '闵行') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '江场') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '金桥') {
            $row['store_name'] = '';
        }
        if ($row['store_name'] == '黄浦') {
            $row['store_name'] = '';
        }

        // if (strpos($row['remark'], '无接触配送') === false) {
        //     $row['remark'] = $row['remark'] . '[无接触配送]';
        // }

        // if ($row['wallet_given_fee'] > 0) {//钱包赠送金额支付
        //     $row['original_amount'] = bcsub($row['original_amount'], $row['wallet_given_fee'], 2 );
        // }

        // if ($row['wallet_random_fee'] > 0) {//钱包支付随机立减金额
        //     $row['original_amount'] = bcsub($row['original_amount'], $row['wallet_random_fee'], 2 );
        // }
		$row['extra_attr_obj'] = json_decode($row['extra_attr'], true);// 转成数组

        if(!$row['remark']){
            $row['remark'] = '';
        }

        foreach ($row['extra_attr_obj'] as $k => $v) {
            $row['remark'] = $row['remark']."(".$v.")";
        }

        if($row['enterprise_id']=='425'){
            $row['remark'] = '';
            foreach ($row['extra_attr_obj'] as $k => $v) {
                if($k=='nianji') {
                    $row['remark'] = $row['remark'].$v;
                }
            }
            foreach ($row['extra_attr_obj'] as $k => $v) {
                if($k=='banji') {
                    $row['remark'] = $row['remark'].$v;
                }
            }
        }

        $data = array(
            'ordersn' => $row['sn'], //订单号
            'clientname' => $row['consignee'], //联系人姓名
            'address' => "[私定]".$row['point_address'], //送餐地址
            'telphone' => $row['mobile'], //收货人手机
            'custdate' => $row['arrive_day'], //送餐日期
            'custtime' => $row['arrive_time'], //送餐时间
            'telname' => '私定', //接单员
            'rectime' => date('H:i:s'), //接单时间
            'recdate' => date('Y-m-d'), //接单日期
            'beizhu' => $row['remark'], //订单备注
			
            'totalmoney' => $row['original_amount'], //订单总金额
            'shouldmoney' => $row['pay_status'] == 0 ? $row['order_amount'] : 0, //应收金额
            'paidmoney' => $row['pay_status'] == 1 ? $row['order_amount'] : 0, //已付金额
            'invoiceheader' => $row['invo_title'] ? $row['invo_title'] : '', //发票抬头
            'invoicebody' => $row['invo_title'] ? '工作餐' : '', //发票内容
            'gmf_nsrsbh' => $row['invo_taxno'] ? $row['invo_taxno'] : '', //纳税人识别号
            'gmf_dzdh' => '', //购买方地址电话
            'gmf_yhzh' => '', //购买方银行账户
            'invoicetype' => $row['invo_title'] ? '电子票' : '',
            'shippingname' => '自配送', //送餐费名称
            // 'shippingmoney'    => $row['deliver_fee'],//送餐费金额
            'company' => '', //$row['store_name'], //分公司门店   $row['store_name']
            'ordertxt' => '', //商品简述
            'longitude' => $row['lng'] ?  $row['lng'] : 0, //订单坐标经度
            'latitude' => $row['lat'] ? $row['lat'] : 0, //订单坐标维度
            'ap' => $row['arrive_time'] >= '15:00:00' ? '下午' : '上午', //午别
            'state' => '订餐',
            'origin' => '私定', //订单来源
            'domain' => $domain, //配送地区标识
        );

        if($row['store_name']=='丽华'){
            $data['company'] = $row['store_name'];
        }



        try {
            $haveorder = $orderModel->where(array('ordersn' => $row['sn']))->find();
            // var_dump($row['sn']);
            // var_dump($haveorder);
            if ($haveorder) {
                return false;
            } else {
                // var_dump($data);
                $orderformid = $orderModel->add($data);
                // var_dump($orderformid);
                // var_dump($orderModel->getLastSql());
            }
        } catch (\Exception $e) {
            $this->addLog('订单' . $row['sn'] . '写入失败：' . $orderModel->getLastSql());
            var_dump('订单' . $row['sn'] . '写入失败：' . $orderModel->getLastSql());
            return false;
        }
        
        // 写订单商品表
        $boxArr = array();
        $data = array();
        $orderTxt = array();
        $goodsMoney = $row['goods_amount']; //商品总金额
        if ($goodsList) {
            foreach ($goodsList as $k => $v) {
                $shortName = $v['short_name'] ?: $v['name'];
                $data[] = array(
                    'orderformid' => $orderformid, //订单id
                    'ordersn' => $row['sn'], //订单号
                    'code' => '', //商品code
                    'name' => $v['name'], //商品名称
                    'shortname' => $shortName, //商品简称
                    'price' => $v['price'], //商品单价
                    'number' => $v['num'], //商品数量
                    'money' => $v['num_price'], //商品金额
                    'domain' => $domain, //配送地区标识
                );
                $orderTxt[] = $v['num'] . '×' . $shortName; //商品简述

                $boxFee = round($v['box_fee'], 2); //餐盒费
                if ($boxFee > 0) {
                    $boxArr["$boxFee"][] = $v['num'];
                }
            }

            // 餐盒费写入商品表
            if (!empty($boxArr)) {
                foreach ($boxArr as $k => $v) {
                    $num = array_sum($v);
                    $name = $k . '元餐盒费';
                    $data[] = array(
                        'orderformid' => $orderformid,
                        'ordersn' => $row['sn'],
                        'code' => '',
                        'name' => $name,
                        'shortname' => $name,
                        'price' => $k,
                        'number' => $num,
                        'money' => $k * $num,
                        'domain' => $domain,
                    );
                    $orderTxt[] = $num . '×' . $name;
                    $goodsMoney += $k * $num;
                }
            }

            // 送餐费写入商品表
            if ($row['deliver_fee'] > 0) {
                $data[] = array(
                    'orderformid' => $orderformid,
                    'ordersn' => $row['sn'],
                    'code' => '',
                    'name' => round($row['deliver_fee'], 2) . 'S',
                    'shortname' => round($row['deliver_fee'], 2) . 'S',
                    'price' => $row['deliver_fee'],
                    'number' => 1,
                    'money' => $row['deliver_fee'],
                    'domain' => $domain,
                );
                $orderTxt[] = '1×' . round($row['deliver_fee'], 2) . 'S';
                $goodsMoney += $row['deliver_fee'];
            }
            if ($data) {
                $goodsModel->addAll($data);
            }
        }

        // 写活动表
        if ($row['enough_fee'] > 0) { //满减
            $this->activity($orderformid, $row['sn'], $row['enough_name'], $row['enough_fee'], $conn);
        }
        if ($row['bonus_fee'] > 0) { //红包
            $this->activity($orderformid, $row['sn'], $row['bonus_name'], $row['bonus_fee'], $conn);
        }
        if ($row['wallet_given_fee'] > 0) { //钱包赠送金额支付
            $this->activity($orderformid, $row['sn'], '钱包赠送金额支付', $row['wallet_given_fee'], $conn);
        }
        if ($row['wallet_random_fee'] > 0) { //钱包支付随机立减金额
            $this->activity($orderformid, $row['sn'], '钱包支付随机立减', $row['wallet_random_fee'], $conn);
        }
        if ($row['wallet_packing_fee'] > 0) { //钱包支付免餐盒费
            $this->activity($orderformid, $row['sn'], '钱包支付免餐盒费', $row['wallet_packing_fee'], $conn);
        }

        $activity_money = 0;
        if ($actList) {
            foreach ($actList as $k => $v) {
                $this->activity($orderformid, $v['order_sn'], $v['name'], $v['discount'], $conn);
                $activity_money += $v['discount'];
            }
        }

        // 写支付表
        if ($payList) {
            foreach ($payList as $k => $v) {

                // if ($row['wallet_given_fee'] > 0) {//钱包赠送金额支付
                //     $v['pay_money'] = bcsub($v['pay_money'], $row['wallet_given_fee'], 2 );
                // }

                // if ($row['wallet_random_fee'] > 0) {//钱包支付随机立减金额
                //     $v['pay_money'] = bcsub($v['pay_money'], $row['wallet_random_fee'], 2 );
                // }

                $payModel->add(
                    array(
                        'orderformid' => $orderformid, //订单ID
                        'ordersn' => $row['sn'], //订单号
                        'paymentid' => 0, //支付ID
                        'name' => $v['pay_name'], //支付名称
                        'money' => $v['pay_money'], //支付金额
                        'note' => '', //支付备注
                    )
                );
                // 结账修正，改为orderfinance
                // 插入结账的数据
                $financeModel->add(
                    array(
                        'ordersn' => $row['sn'],
                        'name' => $v['pay_name'],
                        'money' => $v['pay_money'],
                        'date' => date('Y-m-d H:i:s'),
                        'domain' => $domain,
                    )
                );
            }
        }

        // 更新订单信息
        $orderTxt = join(' ', $orderTxt);
        $data = array(
            'ordertxt' => $orderTxt,
            'goodsmoney' => $goodsMoney,
        );
        $orderModel->where(array('orderformid' => $orderformid))->save($data);

        // 写状态表
        $stateModel->add(
            array(
                'create' => 1,
                'createtime' => date('Y-m-d H:i:s'),
                'createcontent' => '网站输入',
                'orderformid' => $orderformid,
                'ordersn' => $row['sn'],
                'domain' => $domain,
            )
        );

        // 写操作到action中
        $actionModel->add(
            array(
                'orderformid' => $orderformid,
                'ordersn' => $row['sn'],
                'action' => '网站下载 ' . $row['address'] . ' ' . $orderTxt . ' ' . $row['remark'],
                'logtime' => date('H:i:s'),
                'domain' => $domain,
            )
        );

        // 写操作到action中
        if(!!$row['store_name']){
            $actionModel->add(
                array(
                    'orderformid'    => $orderformid,
                    'ordersn'        => $row['sn'],
                    'action'         => '订单已分配到【' . $row['store_name'] . '】',
                    'logtime'        => date('H:i:s'),
                    'domain'         => $domain,
                )
            );
        }

        // 写营收状态表，驱动营收结账
        $yingshouModel->add(
            array(
                'orderformid' => $orderformid,
                'ordersn' => $row['sn'],
                'status' => 0,
                'assisstatus' => 1,
                'domain' => $domain,
            )
        );

        // 写网站状态接口表
        $webstatusModel->add(
            array(
                'ordersn' => $row['sn'],
                'type' => 1, //表示派单
                'content' => '订单确认',
                'date' => date('Y-m-d H:i:s'),
                'origin' => $row['platform'],
                'domain' => $domain,
            )
        );

        // 通知客户的消息
        $notifyModel->add(
            array(
                'ordersn' => $row['sn'],
                'telphone' => '',
                'app_tk' => '',
                'contenttype' => 'confirm',
                'origin' => $row['platform'],
                'domain' => $domain,
            )
        );

        // 保存发票
        if ($row['invo_title']) {
            $invoiceModel->add(
                array(
                    'header' => $row['invo_title'],
                    'body' => '工作餐',
                    'gmf_nsrsbh' => $row['invo_taxno'],
                    'gmf_dzdh' => '',
                    'gmf_yhzh' => '',
                    'type' => 3, // 2普通发票，3电子发票
                    'ordersn' => $row['sn'],
                    'orderformtxt' => $row['address'] . ' ' . $orderTxt,
                    'ordermoney' => $row['original_amount'] - $row['enough_fee'] - $row['bonus_fee'] - $row['wallet_given_fee'] - $activity_money,
                    'ordertime' => date('H:i:s'),
                    'state' => '未开',
                    'company' => $row['store_name'],
                    'domain' => $domain,
                )
            );
        }

        return true;
    }

    // 活动表及结账
    public function activity($orderFormId, $orderSn, $activityName, $activityMoney, $conn)
    {
        M('orderactivity', 'rms_', $conn)->add(
            array(
                'orderformid' => $orderFormId,
                'ordersn' => $orderSn,
                'activityid' => 0,
                'name' => $activityName,
                'money' => $activityMoney,
                'note' => '',
            )
        );
        // 为了结账的需要，活动费
        M('orderfinance', 'rms_', $conn)->add(
            array(
                'ordersn' => $orderSn,
                'name' => '官网活动费',
                'money' => $activityMoney,
                'date' => date('Y-m-d H:i:s'),
                'domain' => $this->domain, //原本都存的是空值
            )
        );
    }

    // 获取rms中webstate, 写入网站中
    public function setBjWebStatus()
    {
        $this->setStatus('bj');
    }

    public function setShWebStatus()
    {
        $this->setStatus('sh');
    }

    public function setCzWebStatus()
    {
        $this->setStatus('cz');
    }

    public function setNjWebStatus()
    {
        $this->setStatus('nj');
    }

    public function setSzWebStatus()
    {
        $this->setStatus('sz');
    }

    public function setWxWebStatus()
    {
        $this->setStatus('wx');
    }

    public function setGzWebStatus()
    {
        $this->setStatus('gz');
    }

    // 设置状态 /Home/LihuaApp/setStatus/city/cz
    public function setStatus($city = 'cz')
    {
        $this->logFile = C('LOG_PATH') . 'newSetStatus-' . $city . '-' . date('y_m_d') . '.log';
        if (isset($this->cityIds[$city])) {
            $city_id = $this->cityIds[$city];
            $domain = $this->domains[$city_id]; // 配送地区标识
            $conn = $city == 'bj' ? $this->connBj : $this->conn;
            $webstatusModel = M('webstatus', 'rms_', $conn); // 网站状态表

            //老系统 订单状态：0=客户下单,1=订单确认,2=分配到配送点,3=派单给送餐员,4=订单配送完成,8=下单失败,10=逻辑删除
            //新系统 订单状态：0=已取消,1=待支付,2=待确认,3=待分配,4=待派送,5=派送中,6=已完成
            $newStatus = array(0 => 2, 1 => 3, 2 => 4, 3 => 5, 4 => 6, 8 => 0, 10 => 0);

            $where = "`domain`='{$domain}' AND `state`=0 AND (`origin`='app' OR `origin`='xcx')";
            // var_dump($where);
            $list = $webstatusModel->where($where)->limit(1000)->select();
            var_dump($webstatusModel->getLastSql());
            //var_dump($list);
            if ($list) {
                foreach ($list as $k => $v) {
                    if (isset($newStatus[$v['type']])) {
                        // 更新来源订单状态
                        $res = $this->httpReq(
                            $this->statusApi,
                            array(
                                'sn' => $v['ordersn'], //订单号
                                'status' => $newStatus[$v['type']], //状态类型
                                'des' => $v['content'], //配送状态描述
                            )
                        );
                        var_dump(array(
                            'sn' => $v['ordersn'], //订单号
                            'status' => $newStatus[$v['type']], //状态类型
                            'des' => $v['content'], //配送状态描述
                        ));
                        // var_dump($res);
                        $res = json_decode($res, true);
                        var_dump($res);
                        if ($res['code'] == 0) {
                            $this->addLog("修改订单{$v['ordersn']}状态失败：{$res['msg']}");
                        } else {
                            $webstatusModel->where(
                                array(
                                    'ordersn' => $v['ordersn'],
                                    'webstatus' => $v['webstatus'],
                                )
                            )->save(array('state' => 1));
                        }
                        $webstatusModel->where(
                            array(
                                'ordersn' => $v['ordersn'],
                                'webstatus' => $v['webstatus'],
                            )
                        )->save(array('state' => 1));
                    }
                }
            }
        } else {
            $this->addLog("{$city}城市不存在");
            die("{$city}城市不存在");
        }
    }

    // 写日志
    public function addLog($msg)
    {
        Log::write($msg, Log::INFO, '', $this->logFile);
    }

    // http请求
    public function httpReq($url, $data = null)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        if ($data) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:"));
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    // 空方法
    public function _empty()
    {
        exit;
    }
    
    
    // 获取合并订单 /Home/LihuaApp/getOrder/city/cz
    // /alidata/server/php/bin/php /home/rmsdaemon/index.php  Home/privateOrder/getCollectOrder
    public function getCollectOrder()
    {
        $this->logFile = C('LOG_PATH') . 'privateGetOrder-'  . '-' . date('y_m_d') . '.log';
        $res = $this->httpReq($this->orderCollectApi, array('status' => 3, 'exporttype' => 3)); 
        
        $res = json_decode($res, true);
        if (json_last_error()) {
            $msg = "订单数据JSON解析失败";
            $this->addLog($msg);
            die($msg);
        }
        if ($res['code'] == 0) {
            $this->addLog($res['msg']);
            die($res['msg']);
        }
        $data = $res['data']; // 订单数据
        
        foreach ($data as $value) {
            $conn = $value['orderList']['city_id'] == 1 ? $this->connBj : $this->conn;
            $orderList = isset($value['orderList']) ? $value['orderList'] : null;
            $goodsList = isset($value['goodsList']) ? $value['goodsList'] : null;
            $payList = isset($value['payList']) ? $value['payList'] : null;
            $actList = isset($value['actList']) ? $value['actList'] : null;
            
            // 订单数据写入RMS
            $addRes = $this->add2rms($orderList, $goodsList, $payList, $actList, $conn);
                     
            // 更新来源订单状态为已确认
            $res = $this->httpReq(
                $this->statusCollectApi,
                array(
                    'sn' => implode(',',$value['sn']), //订单号
                    'status' => 4, //状态类型
                    'des' => '订单确认', //配送状态描述
                )
                );
            //var_dump($res);
            $res = json_decode($res, true);
            if ($res['code'] == 0) {
                $this->addLog("修改订单{$value['sn']}状态为 订单确认 失败：{$res['msg']}");
            }
        }
    }
}
